package com.yiqi.lottery.feature.basketball.adapter;


import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.suozhang.framework.utils.T;
import com.yiqi.lottery.R;
import com.yiqi.lottery.common.util.DataManager;
import com.yiqi.lottery.entity.bo.BallMatchBo;
import com.yiqi.lottery.feature.basketball.view.BasketballAllTypeActivity;
import com.yiqi.lottery.framework.DataServer;

import java.util.ArrayList;
import java.util.List;

public class BasketballMatchSelectedAdapter extends BaseQuickAdapter<List<BallMatchBo.MatchInfo>, BaseViewHolder> {

    private Context mContext;
    private int mCurrentBetType;
    private OnMatchSelectListener mListener;

    public BasketballMatchSelectedAdapter(Context context, int currentBetType) {
        super((currentBetType == 0 || currentBetType == 1 || currentBetType == 3) ?
                R.layout.item_basketball_match_selected :
                (currentBetType == 2 ? R.layout.item_basketball_match_selected_3 : R.layout.item_basketball_match_selected_4));
        mContext = context;
        mCurrentBetType = currentBetType;
    }


    //            case 0://胜负
//            case 1://让分胜负
//            case 2://胜分差
//            case 3://大小分
//            case 4://混合过关
//            case 5://单关
    @Override
    protected void convert(BaseViewHolder helper, List<BallMatchBo.MatchInfo> item) {
        String no;
        if (item.get(0).getPlay() == null) {
            no = item.get(0).getNo();
        } else {
            no = item.get(0).getPlay().getNo();
        }
        ((TextView) helper.getView(R.id.tv1_1)).setText(TextUtils.isEmpty(no) ? String.format("%03d", (helper.getAdapterPosition() + 1)) : no.substring(1));
        helper.getView(R.id.tv1_2).setTag(helper.getAdapterPosition());
        helper.getView(R.id.tv1_2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                remove((int) view.getTag());
                notifyDataSetChanged();
            }
        });
        if (mCurrentBetType == 0 || mCurrentBetType == 1 || mCurrentBetType == 3) {
            resetAdapterView1(item.get(0), helper.getView(R.id.ll_parent));
        } else if (mCurrentBetType == 2) {
            resetAdapterView2(item.get(0), helper.getView(R.id.ll_parent), helper.getView(R.id.tvDan));
        } else {
            resetAdapterView3(item, helper.getView(R.id.ll_parent), helper.getView(R.id.tvDan), helper.getView(R.id.tv6_1));
        }
        if (mListener != null) {
            mListener.reset();
        }
    }

    private void resetAdapterView2(BallMatchBo.MatchInfo matchInfo, LinearLayout ll_parent, TextView tvDan) {
        Resources resources = mContext.getResources();
        if(getData().size()==1){
            tvDan.setVisibility(View.INVISIBLE);
        }else {
            tvDan.setVisibility(View.VISIBLE);
            tvDan.setTag(matchInfo);
            if (matchInfo.isDan()) {
                tvDan.setBackgroundColor(resources.getColor(R.color.primary_highlight));
                tvDan.setTextColor(resources.getColor(R.color.primary_comparison));
            } else {
                tvDan.setBackgroundColor(resources.getColor(R.color.primary_comparison));
                tvDan.setTextColor(resources.getColor(R.color.text_primary));
            }
            tvDan.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    BallMatchBo.MatchInfo matchInfo = (BallMatchBo.MatchInfo) view.getTag();
                    if (!matchInfo.isDan() && !isCanDan()) {
                        return;
                    }
                    matchInfo.setDan(!matchInfo.isDan());
                    if (matchInfo.isDan()) {
                        view.setBackgroundColor(resources.getColor(R.color.primary_highlight));
                        ((TextView) view).setTextColor(resources.getColor(R.color.primary_comparison));
                    } else {
                        view.setBackgroundColor(resources.getColor(R.color.primary_comparison));
                        ((TextView) view).setTextColor(resources.getColor(R.color.text_primary));
                    }
                    if (mListener != null) {
                        mListener.reset();
                    }
                }
            });
        }

        ll_parent = (LinearLayout) ll_parent.getChildAt(1);
        ((TextView) ll_parent.getChildAt(0)).setText(Html.fromHtml(matchInfo.getAn() + "  VS  " + matchInfo.getHn()));
        if (matchInfo.getIsCheck() == null && !matchInfo.getSpTypeGg().isEmpty()) {
            boolean[] isCheck = new boolean[matchInfo.getSpTypeGg().size()];
            matchInfo.setIsCheck(isCheck);
        }
        int childCount = ll_parent.getChildCount();
        LinearLayout ll;
        int index = 0;
        for (int i = childCount - 1; i > 0; --i) {
            if (!(ll_parent.getChildAt(i) instanceof LinearLayout)) {
                continue;
            }
            ll = (LinearLayout) ((LinearLayout) ll_parent.getChildAt(i)).getChildAt(1);
            index = resetSFCItem(matchInfo, index, (LinearLayout) ll.getChildAt(0));
            index = resetSFCItem(matchInfo, index, (LinearLayout) ll.getChildAt(1));

        }
    }

    private int resetSFCItem(BallMatchBo.MatchInfo matchInfo, int index, LinearLayout ll) {
        LinearLayout itemView;
        Resources resources = mContext.getResources();
        for (int j = 0; j < ll.getChildCount(); ++j) {
            if (!(ll.getChildAt(j) instanceof LinearLayout)) {
                continue;
            }
            itemView = (LinearLayout) ll.getChildAt(j);
            itemView.setTag(index);
            itemView.setTag(R.id.tag, matchInfo);
            if (!matchInfo.getSpTypeGg().isEmpty()) {
                itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        int index = (int) view.getTag();
                        BallMatchBo.MatchInfo matchInfo = (BallMatchBo.MatchInfo) view.getTag(R.id.tag);
                        matchInfo.setCheck(index, !matchInfo.isCheck(index));
                        LinearLayout itemView = (LinearLayout) view;
                        if (matchInfo.isCheck(index)) {
                            itemView.setBackgroundColor(resources.getColor(R.color.primary_highlight));
                            ((TextView) itemView.getChildAt(0)).setTextColor(resources.getColor(R.color.primary_comparison));
                            ((TextView) itemView.getChildAt(1)).setTextColor(resources.getColor(R.color.primary_comparison));
                        } else {
                            itemView.setBackgroundColor(resources.getColor(R.color.primary_comparison));
                            ((TextView) itemView.getChildAt(0)).setTextColor(resources.getColor(R.color.text_primary));
                            ((TextView) itemView.getChildAt(1)).setTextColor(resources.getColor(R.color.text_secondary));
                        }
                        if (mListener != null) {
                            mListener.reset();
                        }
                    }
                });
            }
            ((TextView) itemView.getChildAt(0)).setText(DataServer.BASKET_BALL_SFC[index % 6]);
            ((TextView) itemView.getChildAt(1)).setText(matchInfo.getSpTypeGg().isEmpty() ? "--" : matchInfo.getSpTypeGg().get(index));
            if (matchInfo.isCheck(index)) {
                itemView.setBackgroundColor(resources.getColor(R.color.primary_highlight));
                ((TextView) itemView.getChildAt(0)).setTextColor(resources.getColor(R.color.primary_comparison));
                ((TextView) itemView.getChildAt(1)).setTextColor(resources.getColor(R.color.primary_comparison));
            } else {
                itemView.setBackgroundColor(resources.getColor(R.color.primary_comparison));
                ((TextView) itemView.getChildAt(0)).setTextColor(resources.getColor(R.color.text_primary));
                ((TextView) itemView.getChildAt(1)).setTextColor(resources.getColor(R.color.text_secondary));
            }
            ++index;
        }
        return index;
    }

    private void resetAdapterView3(List<BallMatchBo.MatchInfo> matchInfos, LinearLayout ll_parent, TextView tvDan, TextView tvRanfen) {
        Resources resources = mContext.getResources();
        if(mCurrentBetType==5 || getData().size()==1){
            tvDan.setVisibility(View.INVISIBLE);
        }else {
            tvDan.setVisibility(View.VISIBLE);
            tvDan.setTag(matchInfos.get(0));
            if (matchInfos.get(0).isDan()) {
                tvDan.setBackgroundColor(resources.getColor(R.color.primary_highlight));
                tvDan.setTextColor(resources.getColor(R.color.primary_comparison));
            } else {
                tvDan.setBackgroundColor(resources.getColor(R.color.primary_comparison));
                tvDan.setTextColor(resources.getColor(R.color.text_primary));
            }
            tvDan.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    BallMatchBo.MatchInfo matchInfo = (BallMatchBo.MatchInfo) view.getTag();
                    if (!matchInfo.isDan() && !isCanDan()) {
                        return;
                    }

                    matchInfo.setDan(!matchInfo.isDan());
                    if (matchInfo.isDan()) {
                        view.setBackgroundColor(resources.getColor(R.color.primary_highlight));
                        ((TextView) view).setTextColor(resources.getColor(R.color.primary_comparison));
                    } else {
                        view.setBackgroundColor(resources.getColor(R.color.primary_comparison));
                        ((TextView) view).setTextColor(resources.getColor(R.color.text_primary));
                    }
                    if (mListener != null) {
                        mListener.reset();
                    }
                }
            });
        }
        BallMatchBo.MatchInfo play = matchInfos.get(0).getPlay();
        ((TextView) ll_parent.getChildAt(0)).setText(Html.fromHtml(play.getAn() + "  VS  " + play.getHn()));
        int childCount = ll_parent.getChildCount();
        LinearLayout ll, llQita;
        TextView tv;
        ArrayList<String> list;
        for (int i = 1; i < childCount; ++i) {
            if (!(ll_parent.getChildAt(i) instanceof LinearLayout)) {
                continue;
            }
            ll = (LinearLayout) ll_parent.getChildAt(i);
            if (i == childCount - 1) {
                llQita = (LinearLayout) ll.getChildAt(1);
                int count = llQita.getChildCount();
                for (int k = 0; k < count; ++k) {
                    llQita.getChildAt(k).setVisibility(View.GONE);
                }
                int size = matchInfos.size();
                ArrayList<String> selectPeilvs = new ArrayList<>();
                String[] dataType = null;
                boolean[] isCheck;
                for (int j = 2; j < size; ++j) {
                    play = matchInfos.get(j).getPlay();
                    list = play.getSpTypeGg();
                    if (j == 2) {
                        dataType = DataServer.BASKET_BALL_DXF;
                    } else if (j == 3) {
                        dataType = DataServer.BASKET_BALL_SFC_2;
                    }
                    isCheck = play.getIsCheck();
                    if(isCheck!=null) {
                        for (int k = 0; k < isCheck.length; ++k) {
                            if (isCheck[k]) {
                                selectPeilvs.add(dataType[k] + "_" + list.get(k));
                            }
                        }
                    }
                }
                if (selectPeilvs.isEmpty()) {
                    LinearLayout llQitaItem;
                    if (count == 0) {
                        llQitaItem = (LinearLayout) LayoutInflater.from(mContext).inflate(R.layout.item_football_match_selected_qita, null);
                        llQita.addView(llQitaItem);
                    } else {
                        llQitaItem = (LinearLayout) llQita.getChildAt(0);
                        llQitaItem.setVisibility(View.VISIBLE);
                    }
                    llQitaItem.getChildAt(1).setVisibility(View.GONE);
                    llQitaItem.getChildAt(2).setVisibility(View.GONE);
                    LinearLayout item = (LinearLayout) llQitaItem.getChildAt(0);
                    item.setBackgroundColor(resources.getColor(R.color.primary_comparison));
                    TextView tv1 = (TextView) item.getChildAt(0);
                    tv1.setText("未选择其他玩法");
                    tv1.setTextColor(mContext.getResources().getColor(R.color.text_primary));
                    item.getChildAt(1).setVisibility(View.GONE);
                    item.setTag(matchInfos);
                    item.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            DataManager.getInstance().setTempObject(view.getTag());
                            Intent intent = new Intent(mContext, BasketballAllTypeActivity.class);
                            intent.putExtra("current_type", mCurrentBetType);
                            mContext.startActivity(intent);
                        }
                    });
                } else {
                    int selectNum = selectPeilvs.size();
                    LinearLayout llQitaItem;
                    int row = selectNum % 2 == 0 ? selectNum / 2 : selectNum / 2 + 1;
                    for (int k = 0; k < row; ++k) {
                        if (llQita.getChildCount() <= k) {
                            llQitaItem = (LinearLayout) LayoutInflater.from(mContext).inflate(R.layout.item_football_match_selected_qita, null);
                            llQita.addView(llQitaItem);
                        } else {
                            llQitaItem = (LinearLayout) llQita.getChildAt(k);
                            llQitaItem.setVisibility(View.VISIBLE);
                        }
                        resetQitaItm((LinearLayout) llQitaItem.getChildAt(0), selectPeilvs.get(k * 2), matchInfos);
                        if (k * 2 + 1 < selectNum) {
                            resetQitaItm((LinearLayout) llQitaItem.getChildAt(1), selectPeilvs.get(k * 2 + 1), matchInfos);
                        } else {
                            llQitaItem.getChildAt(1).setVisibility(View.INVISIBLE);
                            llQitaItem.getChildAt(2).setVisibility(View.INVISIBLE);
                        }
                        llQitaItem.getChildAt(2).setVisibility(View.GONE);
                    }
                }
                continue;
            }
            if (i == 1) {
                play = matchInfos.get(0).getPlay();
                if (mCurrentBetType == 4) {
                    list = play.getSpTypeGg();
                } else {
                    list = play.getSpTypeDg();
                }
            } else {
                play = matchInfos.get(1).getPlay();
                if (mCurrentBetType == 4) {
                    list = play.getSpTypeGg();
                } else {
                    list = play.getSpTypeDg();
                }
                if (TextUtils.isEmpty(play.getHand2())) {
                    tvRanfen.setText("");
                } else {
                    double hand = Double.parseDouble(play.getHand2());
                    tvRanfen.setText(
                            Html.fromHtml((hand > 0 ? "<font color=\"#D83220\">+" : "<font color=\"#43C066\">") + play.getHand2() + "</font>"));
                }
            }
            for (int j = 0; j < ll.getChildCount(); ++j) {
                if (!(ll.getChildAt(j) instanceof TextView)) {
                    if (mCurrentBetType == 5 && list.isEmpty()) {
                        ll.getChildAt(j).setVisibility(View.GONE);
                    } else {
                        ll.getChildAt(j).setVisibility(View.VISIBLE);
                    }
                    continue;
                }
                int index = j / 2;
                tv = (TextView) ll.getChildAt(j);
                if (i == 1) {
                    if (j == 0) {
                        tv.setText("0");
                        continue;
                    }
                } else {
                    if (j == 0) {
                        tv.setText("让");
                        continue;
                    }
                }

                if (!list.isEmpty()) {
                    tv.setVisibility(View.VISIBLE);
                    if (index == 0) {
                        tv.setText("客胜" + list.get(1));
                    } else if (index == 1) {
                        tv.setText("主胜" + list.get(0));
                    }
                } else {
                    if (index == 0) {
                        tv.setText("未开售");
                    } else if (index == 1) {
                        tv.setVisibility(View.GONE);
                    } else {
                        tv.setVisibility(View.GONE);
                    }
                }
                tv.setTag(index == 1 ? 0 : 1);
                tv.setTag(R.id.tag, play);
                if (!list.isEmpty()) {
                    tv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            int index = (int) view.getTag();
                            BallMatchBo.MatchInfo play = (BallMatchBo.MatchInfo) view.getTag(R.id.tag);
                            play.setCheck(index, !play.isCheck(index));
                            TextView itemView = (TextView) view;
                            if (play.isCheck(index)) {
                                itemView.setBackgroundColor(resources.getColor(R.color.primary_highlight));
                                itemView.setTextColor(resources.getColor(R.color.primary_comparison));
                            } else {
                                itemView.setBackgroundColor(resources.getColor(R.color.primary_comparison));
                                itemView.setTextColor(resources.getColor(R.color.text_primary));
                            }
                            if (mListener != null) {
                                mListener.reset();
                            }
                        }
                    });
                }
                if (!list.isEmpty() && play.isCheck(index == 1 ? 0 : 1)) {
                    tv.setBackgroundColor(resources.getColor(R.color.primary_highlight));
                    tv.setTextColor(resources.getColor(R.color.primary_comparison));
                } else {
                    tv.setBackgroundColor(resources.getColor(R.color.primary_comparison));
                    tv.setTextColor(resources.getColor(R.color.text_primary));
                }
            }
        }
    }

    private void resetQitaItm(LinearLayout item, String peilv, List<BallMatchBo.MatchInfo> matchInfos) {
        String[] strs = peilv.split("_");
        item.setVisibility(View.VISIBLE);
        TextView tv = (TextView) item.getChildAt(0);
        tv.setText(strs[0]);
        tv.setTextColor(mContext.getResources().getColor(R.color.primary_comparison));

        tv = (TextView) item.getChildAt(1);
        tv.setTextColor(mContext.getResources().getColor(R.color.primary_comparison));
        tv.setVisibility(View.VISIBLE);
        tv.setText(strs[1]);
        item.setTag(matchInfos);
        item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DataManager.getInstance().setTempObject(view.getTag());
                Intent intent = new Intent(mContext, BasketballAllTypeActivity.class);
                intent.putExtra("current_type", mCurrentBetType);
                mContext.startActivity(intent);
            }
        });
        item.setBackgroundColor(mContext.getResources().getColor(R.color.primary_highlight));
    }

    private boolean isCanDan() {
        int danNum = getDanNum();
        if (danNum < 7 && danNum < getItemCount() - 1) {
            return true;
        }
        T.showShort("胆的个数要小于所选比赛场次，且小于8个");
        return false;
    }

    public int getDanNum() {
        List<List<BallMatchBo.MatchInfo>> list = getData();
        if (list.isEmpty()) {
            return 0;
        }
        int danNum = 0;
        for (List<BallMatchBo.MatchInfo> matchInfos : list) {
            if (matchInfos.get(0).isDan()) {
                ++danNum;
            }
        }
        return danNum;
    }

    private void resetAdapterView1(BallMatchBo.MatchInfo matchInfo, LinearLayout ll_parent) {
        int childCount = ll_parent.getChildCount();
        LinearLayout itemView;
        Resources resources = mContext.getResources();

        ArrayList<String> list = matchInfo.getSpTypeGg();
        if (matchInfo.getIsCheck() == null && !list.isEmpty()) {
            boolean[] isCheck = new boolean[list.size()];
            matchInfo.setIsCheck(isCheck);
        }
        for (int i = 1; i < childCount; ++i) {
            if (!(ll_parent.getChildAt(i) instanceof LinearLayout)) {
                continue;
            }
            itemView = (LinearLayout) ll_parent.getChildAt(i);
            int index = i / 2;

            itemView.getChildAt(1).setTag(R.id.tag, matchInfo);
            if (matchInfo.getIsCheck() != null) {
                itemView.getChildAt(1).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        int index = (int) view.getTag();
                        BallMatchBo.MatchInfo matchInfo = (BallMatchBo.MatchInfo) view.getTag(R.id.tag);
                        if (index < 2) {
                            matchInfo.setCheck(index, !matchInfo.isCheck(index));
                        } else if (index == 2) {
                            if (!matchInfo.isDan() && !isCanDan()) {
                                return;
                            }
                            matchInfo.setDan(!matchInfo.isDan());
                        }
                        LinearLayout itemView = (LinearLayout) view.getParent();
                        boolean isCheck = (index < 2 && matchInfo.getIsCheck() != null && matchInfo.isCheck(index)) || (index == 2 && matchInfo.isDan());
                        if (isCheck) {
                            view.setBackgroundColor(resources.getColor(R.color.primary_highlight));
                            ((TextView) itemView.getChildAt(1)).setTextColor(resources.getColor(R.color.primary_comparison));
                        } else {
                            view.setBackgroundColor(resources.getColor(R.color.primary_comparison));
                            ((TextView) itemView.getChildAt(1)).setTextColor(resources.getColor(R.color.text_secondary));
                        }
                        if (mListener != null) {
                            mListener.reset();
                        }
                    }
                });
            }

            if (index == 0) {
                ((TextView) itemView.getChildAt(0)).setText(Html.fromHtml(matchInfo.getAn()));
                if (mCurrentBetType == 3) {
                    itemView.getChildAt(1).setTag(0);
                    ((TextView) itemView.getChildAt(1)).setText(list.isEmpty() ? "--" : "大分" + list.get(index));
                } else {
                    itemView.getChildAt(1).setTag(1);
                    ((TextView) itemView.getChildAt(1)).setText(list.isEmpty() ? "--" : "客胜" + list.get(1));
                }
            } else if (index == 1) {
                ((TextView) itemView.getChildAt(0)).setText(Html.fromHtml(matchInfo.getHn()));
                if (mCurrentBetType == 3) {
                    itemView.getChildAt(1).setTag(1);
                    ((TextView) itemView.getChildAt(1)).setText(list.isEmpty() ? "--" : "小分" + list.get(index));
                } else {
                    itemView.getChildAt(1).setTag(0);
                    ((TextView) itemView.getChildAt(1)).setText(list.isEmpty() ? "--" : "主胜" + list.get(0));
                }
            } else if (index == 2) {
                if (TextUtils.isEmpty(matchInfo.getHand2())) {
                    ((TextView) itemView.getChildAt(0)).setText("");
                } else {
                    if (mCurrentBetType == 1) {
                        double hand = Double.parseDouble(matchInfo.getHand2());
                        ((TextView) itemView.getChildAt(0)).setText(
                                Html.fromHtml((hand > 0 ? "<font color=\"#D83220\">+" : "<font color=\"#43C066\">") + matchInfo.getHand2() + "</font>"));
                    } else if (mCurrentBetType == 3) {
                        ((TextView) itemView.getChildAt(0)).setText(
                                Html.fromHtml("<font color=\"#D83220\">" + matchInfo.getHand2() + "</font>"));
                    }
                }
                itemView.getChildAt(1).setTag(index);
            }

            boolean isCheck = (index < 2 && matchInfo.getIsCheck() != null && (mCurrentBetType == 3 ?
                    matchInfo.isCheck(index) : matchInfo.isCheck(index == 1 ? 0 : 1))) || (index == 2 && matchInfo.isDan());

            if (isCheck) {
                itemView.getChildAt(1).setBackgroundColor(resources.getColor(R.color.primary_highlight));
                ((TextView) itemView.getChildAt(1)).setTextColor(resources.getColor(R.color.primary_comparison));
            } else {
                itemView.getChildAt(1).setBackgroundColor(resources.getColor(R.color.primary_comparison));
                ((TextView) itemView.getChildAt(1)).setTextColor(resources.getColor(R.color.text_secondary));
            }
        }
    }

    public void setOnMatchSelectListener(OnMatchSelectListener listener) {
        mListener = listener;
    }

    public interface OnMatchSelectListener {
        public void showPopupWindow(int currentType, BallMatchBo.MatchInfo matchInfo, TextView tv);

        public void reset();
    }
}
